using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.IO;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Assets.Enums;
using System.Globalization;
using VSSDKAssist.Assets.Actions.General;
using VSSDKAssist.Recipes.Common.Helpers;
using Microsoft.VisualStudio.Shell;

namespace VSSDKAssist.Recipes.Common.Actions
{
	/// <summary>
	/// Action that registers/unregisters a VSIP package
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
	public class RegisterVSPackageAction : ActionBase
	{
		#region Public Properties

		private EnvDTE.Project project;

		/// <summary>
		/// Gets or sets the project.
		/// </summary>
		/// <value>The project.</value>
		[Input(Required = true)]
		public EnvDTE.Project Project
		{
			get
			{
				return this.project;
			}
			set
			{
				this.project = value;
			}
		}

		private RegistrationMode registrationMode;

		/// <summary>
		/// Gets or sets the registration mode.
		/// </summary>
		/// <value>The registration mode.</value>
		[Input(Required = true)]
		public RegistrationMode RegistrationMode
		{
			get { return registrationMode; }
			set { registrationMode = value; }
		}

		private VSHive vsHive = VSHive.Experimental;

		/// <summary>
		/// Gets or sets the vs hive.
		/// </summary>
		/// <value>The vs hive.</value>
		[Input(Required = false)]
		public VSHive VSHive
		{
			get { return vsHive; }
			set { vsHive = value; }
		}

		private RegistrationMethod registrationMethod;

		/// <summary>
		/// Gets or sets the registration method.
		/// </summary>
		/// <value>The registration method.</value>
		[Input(Required = false)]
		public RegistrationMethod RegistrationMethod
		{
			get { return registrationMethod; }
			set { registrationMethod = value; }
		}
		#endregion

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string outputPath = this.project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();
			string buildPath = this.project.Properties.Item("LocalPath").Value.ToString();
			string targetName = this.project.Properties.Item("OutputFileName").Value.ToString();
			string assemblyFile = Path.Combine(buildPath, Path.Combine(outputPath, targetName));

			if(File.Exists(assemblyFile))
			{
				using(ExecuteProcessAction executeProcessAction = new ExecuteProcessAction())
				{
					executeProcessAction.Site = this.Site;
					executeProcessAction.FileName =
							Path.Combine(
								Path.Combine(CommonHelper.GetVSSdkInstallPath(), Constants.VSSdkToolRelPath),
								Constants.RegPkgTool);

					switch(this.registrationMode)
					{
						case RegistrationMode.Register:
							//VSIPPackageHelper.RegisterVSPackage(assemblyFile, this.vsHive);
							executeProcessAction.Arguments = GetRegisterArguments(assemblyFile, this.vsHive);
							break;
						case RegistrationMode.Unregister:
							//VSIPPackageHelper.UnRegisterVSPackage(assemblyFile, this.vsHive);
							executeProcessAction.Arguments = GetUnregisterArguments(assemblyFile, this.vsHive);
							break;
						default:
							break;
					}

					executeProcessAction.Execute();
				}
			}
		}

		#region Private Implementation
		private static string GetUnregisterArguments(string assemblyFile, VSHive hive)
		{
			string arguments = string.Empty;

			if(assemblyFile.Contains(" "))
			{
				assemblyFile = string.Concat("\"", assemblyFile, "\"");
			}

			if(hive == VSHive.Standard)
			{
				arguments =
					string.Format(
						CultureInfo.CurrentCulture,
						"/root:{0} /unregister {1}",
						Constants.VSHiveStandard,
						assemblyFile);
			}
			else if(hive == VSHive.Experimental)
			{
				arguments =
					string.Format(
						CultureInfo.CurrentCulture,
						"/root:{0} /unregister {1}",
						Constants.VSHiveExperimental,
						assemblyFile);
			}

			return arguments;
		}

		private string GetRegisterArguments(string assemblyFile, VSHive hive)
		{
			string arguments = string.Empty;

			switch(this.RegistrationMethod)
			{
				case RegistrationMethod.Assembly:
					arguments += "/assembly ";
					break;
				case RegistrationMethod.CodeBase:
					arguments += "/codebase ";
					break;
				case RegistrationMethod.Default:
					break;
				default:
					break;
			}

			if(assemblyFile.Contains(" "))
			{
				assemblyFile = string.Concat("\"", assemblyFile, "\"");
			}

			if(hive == VSHive.Standard)
			{
				arguments +=
					string.Format(
						CultureInfo.CurrentCulture,
						"/root:{0} {1}",
						Constants.VSHiveStandard,
						assemblyFile);
			}
			else if(hive == VSHive.Experimental)
			{
				arguments +=
					string.Format(
						CultureInfo.CurrentCulture,
						"/root:{0} {1}",
						Constants.VSHiveExperimental,
						assemblyFile);
			}

			return arguments;
		}
		#endregion
	}
}